package com.ruoyi.data.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.EditGroupOne;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.domain.DataList;
import com.ruoyi.data.domain.bo.DataListBo;
import com.ruoyi.data.domain.bo.getData.*;
import com.ruoyi.data.domain.vo.DataClassificationVo;
import com.ruoyi.data.domain.vo.DataListVo;
import com.ruoyi.data.domain.vo.DataMd5Vo;
import com.ruoyi.data.service.IDataMd5Service;
import com.ruoyi.data.service.IGetDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取数据操作
 *
 * @author lcx
 * @date 2023-11-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/getData")
@SaIgnore
public class GetDataController extends BaseController {

    private final IGetDataService iGetDataService;

    private final IDataMd5Service iDataMd5Service;



    /* ------------------------------------------------查询------------------------------------------------------------------------ */

    int num = 0;

    /**
     * 根据分类获取一条全量数据
     * @param bo
     * @return
     */
    @GetMapping("/getOneDataList")
    @SaIgnore
    public R<DataList> getOneDataList(@Validated(AddGroup.class) GetDataDataListBo bo) throws Exception {

        return R.ok(iGetDataService.getOneDataList(bo));
    }

    /**
     * 根据分类获取一条全量数据指定字段
     * @param bo
     * @return
     */
    @GetMapping("/getOneDataListByName")
    @SaIgnore
    public R<DataList> getOneDataListByName(@Validated(AddGroup.class) GetDataDataListBo bo) throws Exception {

        return R.ok(iGetDataService.getOneDataList(bo));
    }

    /**
     * 获取分类下数据总数
     * @param bo
     * @return
     * @throws Exception
     */
    @GetMapping("/getOneDataListCount")
    @SaIgnore
    public R<Map> getOneDataListCount(@Validated(AddGroup.class) GetDataDataListBo bo) throws Exception {

        Long oneDataListCount = iGetDataService.getOneDataListCount(bo);

        HashMap map = new HashMap();
        map.put("count", oneDataListCount);

        return R.ok((map) );
    }

    /**
     * 查询分类数据（分页）
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/getOneDataListPage")
    @SaIgnore
    public TableDataInfo<DataList> getOneDataListPage(@Validated(AddGroup.class) GetDataDataListBo bo, PageQuery pageQuery) {

        if (pageQuery.getPageSize() == null){
            pageQuery.setPageSize(10);
        }


        return iGetDataService.queryPageList(bo, pageQuery);
    }


    /**
     * 按字段查询数据
     * @param bo
     * @param pageQuery
     * @return
     */

    @GetMapping("/getOneDataListPageByFieldData")
    @SaIgnore
    public R<List> getOneDataListPageByFieldData(@Validated(EditGroupOne.class) GetDataDataListBo bo, PageQuery pageQuery) {


        return R.ok(iGetDataService.getOneDataListPageByFieldData(bo, pageQuery));
    }


    /* ------------------------------------------------分类------------------------------------------------------------------------ */

    /**
     * 添加分类 分类名称不能重复
     * @param bo
     * @return
     */
    @GetMapping("/addDataClassification")
    @SaIgnore
    public R<Void> addDataClassification(@Validated(AddGroup.class) GetDataClassificationBo bo) {

        return toAjax(iGetDataService.addDataClassification(bo));
    }

    /**
     * 删除分类
     * @param bo
     * @return
     */
    @GetMapping("/delDataClassification")
    @SaIgnore
    public R<Void> delDataClassification(@Validated(AddGroup.class) GetDataClassificationBo bo) {

        return toAjax(iGetDataService.delDataClassification(bo));
    }

    /* ------------------------------------------------数据------------------------------------------------------------------------ */


    /**
     * 指定分类添加数据
     * @param bo
     * @return
     */
    @GetMapping("/addDataList")
    @SaIgnore
    public R<DataList> addDataList(@Validated(AddGroup.class) AddDataListBo bo) {

        return R.ok(iGetDataService.addAndUpdateDataList(bo));
    }

    /**
     * 指定分类修改数据
     * @param bo
     * @return
     */
    @GetMapping("/updateDataList")
    @SaIgnore
    public R<DataList> updateDataList(@Validated(EditGroup.class) AddDataListBo bo) {

        return R.ok(iGetDataService.addAndUpdateDataList(bo));
    }

    /**
     * 使用数据id删除数据
     * @param bo
     * @return
     */
    @GetMapping("/delDataList")
    @SaIgnore
    public R<Void> delDataList(@Validated(EditGroup.class) DelDataListBo bo) {

        return toAjax(iGetDataService.delDataList(bo));
    }

    /**
     * 使用分类名称删除分类下所有数据
     * @param bo
     * @return
     */
    @GetMapping("/delDataListByClassification")
    @SaIgnore
    public R<Void> delDataListByClassification(@Validated(EditGroupOne.class) DelDataListBo bo) {

        return toAjax(iGetDataService.delDataListByClassification(bo));
    }

    /**
     * 转移数据
     * 需要 是否已读  目标分类名称 现在所属分类 数据主键id （与分类名称二选一 有id执行id 没有执行该分类下所有本人数据）
     * @param bo
     * @return
     */
    @GetMapping("/batchPaste")
    @SaIgnore
    public R<Void> batchPaste(@Validated(EditGroupOne.class) BatchPasteBo bo) {

        return toAjax(iGetDataService.batchPaste(bo));
    }



    /* ------------------------------------------------md5------------------------------------------------------------------------ */

    /**
     * 添加用户md5
     * @return
     */
    @GetMapping("/addUserMd5")
    @SaIgnore
    public R<DataMd5Vo> addUserMd5(@Validated(AddGroup.class) UserMd5Bo bo) {
        return R.ok(iDataMd5Service.addUserMd5(bo));
    }


    /**
     * 添加用户md5
     * @return
     */
    @GetMapping("/addDataListToRedis")
    @SaIgnore
    public R<DataMd5Vo> addDataListToRedis(@Validated(AddGroup.class) UserMd5Bo bo) {
        iGetDataService.addDataListToRedis();
        return R.ok();
    }

}
